﻿using System;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;
using Libraries.Entities;

namespace Libraries.Repositories
{
    public class AccountCourseRepository
    {
        private AccountCourse ParseToEntityAccountCourse(IDataRecord dataReader)
        {
            if (dataReader != null)
            {
                return new AccountCourse
                {
                    AccountCourseId = (int)dataReader["AccountCourseId"],
                    EmployeeId = (string)dataReader["EmployeeId"],
                    CourseId = (int)dataReader["CourseId"],
                    CourseName = (string)dataReader["CourseName"],
                    Costs = (decimal?)dataReader["Costs"],
                    CurrencyName = (string)dataReader["CurrencyName"],
                    TimeCommit = (decimal?)dataReader["TimeCommit"],
                    TrainedDuration = (decimal?)dataReader["TrainedDuration"],
                    PassCoverage = (string)dataReader["PassCoverage"],
                    FinalResult = (string)dataReader["FinalResult"],
                    Note = (string)dataReader["Note"],
                    CourseYear = (int)dataReader["CourseYear"],
                    EndDate = dataReader["EndDate"].ToStringOrDefault(),
                    CourseStartDate = dataReader["CourseStartDate"].ToStringOrDefault(),
                    Department = (string)dataReader["Department"],
                    FullName = (string)dataReader["FullName"],
                    LocationName = (string)dataReader["LocationName"],
                    TotalRows = (Int64)dataReader["TotalRows"],
                    AccountName = (string)dataReader["AccountName"],
                    JobTitleName = (string)dataReader["JobTitleName"]
                };
            }
            return null;
        }

        public Collection<AccountCourse> PagingAccountCourse(int pageSize, int pageNumber,
            string accountName, string employeeId, int courseCategoryId, string courseName, string fullName,
            string endDateFrom, string endDateTo, int accountLocation, int accountStatus,
            int contractType, string roleId, string courseYear, int maxMonth, int minMonth, int departmentId = 1)
        {
            var parameters = new Collection<SqlParameter>
                                                      {
                                                          new SqlParameter("@PageSize",pageSize),
                                                          new SqlParameter("@PageNumber",pageNumber),
                                                          new SqlParameter("@AccountName",accountName),
                                                          new SqlParameter("@EmployeeId",employeeId),
                                                          new SqlParameter("@CourseCategoryId",courseCategoryId),
                                                          new SqlParameter("@CourseName",courseName),
                                                          new SqlParameter("@FullName",fullName),
                                                          new SqlParameter("@EndDateFrom",endDateFrom),
                                                          new SqlParameter("@EndDateTo",endDateTo),
                                                          new SqlParameter("@AccountLocation",accountLocation),
                                                          new SqlParameter("@AccountStatus",accountStatus),
                                                          new SqlParameter("@ContractType",contractType),
                                                          new SqlParameter("@RoleId",roleId),
                                                          new SqlParameter("@CourseYear",courseYear),
                                                          new SqlParameter("@MinMonth",minMonth),
                                                          new SqlParameter("@MaxMonth",maxMonth),
                                                          new SqlParameter("@DepartmentId",departmentId)
                                                      };

            Collection<AccountCourse> list = null;
            if (!string.IsNullOrEmpty(ProcedureConfiguration.PagingAccountCourse))
            {
                using (var result = RepositorySql.SqlHelper.ExecuteReader(ProcedureConfiguration.PagingAccountCourse, parameters, ConnectionConfiguration.ConnectionString))
                {
                    if (result != null)
                    {
                        list = new Collection<AccountCourse>();

                        while (result.Read())
                        {
                            list.Add(ParseToEntityAccountCourse(result));
                        }

                    }
                }
            }
            return list;
        }
    }
}
